@extends('Layouts.iframe')
@section('content')
    <style>
        #product-search div{
            line-height: 3;
        }
        #product-search input{
            width: 70%;
        }
        .select-box{
            width: 70%;
        }
    </style>
    <nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 产品管理 <span class="c-gray en">&gt;</span> 产品列表 <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="{{url('admin/product')}}" title="刷新" ><i class="Hui-iconfont">&#xe68f;</i></a></nav>
    <div class="page-container">
        <div class="text-l">
            <form action="" method="get" id="product-search">
                <input type="hidden" name="_token" value="{{csrf_token()}}">
                <div class="col-sm-6 col-md-4">
                    <label for="">产品名称 : </label>
                    <input type="text" class="input-text"  placeholder="输入产品名称进行搜索" value="{{$products->name}}" id="" name="name">
                </div>
                <div class="col-sm-6 col-md-4">
                    <label for="">产品分类 : </label>
                    <span class="select-box" style="width: 35%">
                      <select class="select" size="1" name="category_id_first">
                        <option value="">全部类型</option>
                         <?php $categories=\App\Models\Dictionary::select('id','name')->where(['pid'=>1])->get();?>
                          @if($categories->count())
                              @foreach($categories as $category)
                                <option value="{{$category->id}}" {{$category->id==$products->category_id_first?'selected':''}}>{{$category->name}}</option>
                              @endforeach
                          @endif
                      </select>
                    </span>
                    <span class="select-box" style="width: 35%">
                      <select class="select" size="1" name="category_id_second">
                        <option value="">全部类型</option>
                          @if($products->category_id_first)
                              <?php $second_categories=\App\Models\Dictionary::where(['pid'=>$products->category_id_first])->get();?>
                              @if($second_categories!=null)
                                  @foreach($second_categories as $second_category)
                                      <option value="{{$second_category->id}}" {{$second_category->id==$products->category_id_second?'selected':''}}>{{$second_category->name}}</option>
                                  @endforeach
                              @endif
                          @endif
                      </select>
                    </span>
                </div>
                <div class="col-sm-6 col-md-4">
                    <label for="">所属机构 : </label>
                    <span class="select-box">
                      <select class="select" size="1" name="agent_id">
                        <option value="">全部机构</option>
                          <?php $agents=\App\Models\Agent::select('id','name')->get();?>
                          @if($agents->count())
                              @foreach($agents as $agent)
                                <option value="{{$agent->id}}" {{$agent->id==$products->agent_id?'selected':''}}>{{$agent->name}}</option>
                              @endforeach
                          @endif
                      </select>
                    </span>
                </div>

                <div class="col-sm-6 col-md-4">
                    <label for="">放款额度 : </label>
                    <input type="tel" class="input-text" style="width:30%" value="{{$products->loan_start}}" placeholder="" id="" name="loan_start" onkeyup="if( ! /^-?\d+\.?\d{0,2}$/.test(this.value)){ var s = this.value;this.value=s.substring(0,s.length-1);}" onblur="value=value.replace(/[^\d\.]/g,'')"> 至
                    <input type="tel" class="input-text" style="width:30%" value="{{$products->loan_end}}" placeholder="" id="" name="loan_end" onkeyup="if( ! /^-?\d+\.?\d{0,2}$/.test(this.value)){ var s = this.value;this.value=s.substring(0,s.length-1);}" onblur="value=value.replace(/[^\d\.]/g,'')"> (万)
                </div>
                <div class="col-sm-6 col-md-4">
                    <label for="">年龄要求 : </label>
                    <input type="number" class="input-text" style="width:30%" placeholder="" value="{{$products->age_start}}" id="" name="age_start" onkeyup="value=value.replace(/[^\d\.]/g,'')" onblur="value=value.replace(/[^\d\.]/g,'')"> 至
                    <input type="number" class="input-text" style="width:30%" placeholder="" value="{{$products->age_end}}" id="" name="age_end" onkeyup="value=value.replace(/[^\d\.]/g,'')" onblur="value=value.replace(/[^\d\.]/g,'')"> (岁)
                </div>
                <div class="col-sm-6 col-md-4">
                    <label for="">还款期限 : </label>
                    <input type="number" class="input-text" style="width:30%" placeholder="" value="{{$products->repay_start}}" id="" name="repay_start" onkeyup="value=value.replace(/[^\d\.]/g,'')" onblur="value=value.replace(/[^\d\.]/g,'')"> 至
                    <input type="number" class="input-text" style="width:30%" placeholder="" value="{{$products->repay_end}}" id="" name="repay_end" onkeyup="value=value.replace(/[^\d\.]/g,'')" onblur="value=value.replace(/[^\d\.]/g,'')"> (月)
                </div>
                <div class="col-sm-6 col-md-4">
                    <label for="">还款方式 : </label>
                    <span class="select-box">
                      <select class="select" size="1" name="repay_type">
                        <option value="">--不限--</option>
                        <?php $repays=\App\Models\Dictionary::select('id','name')->where(['pid'=>2])->get();?>
                          @if($repays->count())
                              @foreach($repays as $repay)
                                <option value="{{$repay->id}}" {{$repay->id==$products->repay_type?'selected':''}}>{{$repay->name}}</option>
                              @endforeach
                          @endif
                      </select>
                    </span>
                </div>
                <div class="col-sm-6 col-md-4">
                    <label for="">产品状态 : </label>
                    <span class="select-box">
                      <select class="select" size="1" name="status">
                        <option value="">--不限--</option>
                        <option value="1" {{$products->status=='1'?'selected':''}}>上架中</option>
                        <option value="0" {{$products->status=='0'?'selected':''}}>下架中</option>
                      </select>
                    </span>
                </div>
                <div style="text-align: right">
                    <button type="submit" class="btn btn-success radius" id="" name=""><i class="Hui-iconfont">&#xe665;</i></button>
                </div>
            </form>
        </div>
        @can('product.create')
            <div class="cl pd-5 bg-1 bk-gray mt-20">
                <span class="r">
                    <a class="btn btn-primary radius" href="{{route('product.create')}}">
                        <i class="Hui-iconfont">&#xe600;</i>新增产品
                    </a>
                </span>
            </div>
        @endcan
        <div class="mt-20">
            <table class="table table-border table-bordered table-hover table-bg table-sort">
                <thead>
                <tr class="text-c">
                    <th width="">ID</th>
                    <th width="">产品名称</th>
                    <th width="">产品分类</th>
                    <th width="">所属机构</th>
                    <th width="">放款额度</th>
                    <th width="">年龄要求</th>
                    <th width="">还款期限</th>
                    <th width="">还款方式</th>
                    <th width="">区域</th>
                    <th width="">创建时间</th>
                    <th width="">状态</th>
                    <th width="">操作</th>
                </tr>
                </thead>
                <tbody>
                    @if($products->count())
                        @foreach($products as $product)
                            <tr class="text-c">
                                <td>{{$product->id}}</td>
                                <td>{{$product->name}}</td>
                                <td>{{$product->basic_info['category_name_first'].($product->basic_info['category_name_second']?'-'.$product->basic_info['category_name_second']:'')}}</td>
                                <td>{{$product->basic_info['agent_name']}}</td>
                                <td>{{$product->basic_info['loan_start'].'-'.$product->basic_info['loan_end'].'万'}}</td>
                                <td>{{$product->basic_info['age_start'].'-'.$product->basic_info['age_end'].'岁'}}</td>
                                <td>{{$product->basic_info['repay_start'].'-'.$product->basic_info['repay_end'].'月'}}</td>
                                <td>{{$product->basic_info['repay_type_name']}}</td>
                                <td>{{$product->basic_info['area']}}</td>
                                <td>{{$product->created_at}}</td>
                                <td class="td-status">
                                    @if($product->status==1)
                                        <span class="label label-success radius">上架中</span>
                                    @else
                                        <span class="label label-warning radius">下架中</span>
                                    @endif
                                </td>
                                <td>
                                    @can('product.show')
                                        <a href="{{route('product.show',['id'=>$product->id])}}" class="label label-secondary  radius">查看</a>
                                    @endcan
                                    @can('product.edit')
                                        <a href="{{route('product.edit',['id'=>$product->id])}}" class="label label-primary  radius">编辑</a>
                                    @endcan
                                    @can('product.product_status')
                                        @if($product->status==1)
                                            <a href="#" onClick="product_down('{{$product->id}}','{{$product->status}}')" title="下架" class="label label-warning radius">下架</a>
                                        @else
                                            <a href="#" onClick="product_up('{{$product->id}}','{{$product->status}}')" title="上架" class="label label-success radius">上架</a>
                                        @endif
                                    @endcan
                                    @can('product.destroy')
                                        <a href="#" onClick="product_del(this,'{{$product->id}}')" title="删除" class="label label-danger radius">删除</a>
                                    @endcan
                                </td>
                            </tr>
                        @endforeach
                    @endif
                </tbody>
            </table>
        </div>
        <div id="pull_right">
            <div class="pull-right">
                {!! $products->render() !!}
            </div>
        </div>
    </div>
@endsection
@section('js')
    <script>
        /*一级分类联动二级分类*/
        $('select[name="category_id_first"]').on('change',function () {
            var html="<option value=''>全部类型</option>";
            var id=$(this).val();
            if(!id){
                $('select[name="category_id_second"]').html(html);
                return false;
            }
            var _token="{{csrf_token()}}";
            $.post("{{url('admin/getSecondCategory')}}",{'_token':_token,'id':id},function (data) {
                if(data.length>0){
                    $.each(data,function (key,value) {
                       html += "<option value='"+value.id+"'>"+value.name+"</option>";
                    });
                }
                $('select[name="category_id_second"]').html(html);
            });
        });

        /*产品-添加/编辑*/
        function product_add(title,url,w,h){
            layer_show(title,url,w,h);
        }
        /*产品下架*/
        function product_status(obj, id) {
            layer.confirm('确认要删除吗？', function (index) {
                $.ajax({
                    type: 'POST',
                    url: "{{url('admin/product')}}/"+id,
                    data:{'_method':'DELETE','_token':"{{csrf_token()}}"},
                    dataType: 'json',
                    success: function (data) {
                        if(data.status===200){
                            $(obj).parents("li").remove();
                            layer.msg('删除成功!', {icon: 1, time: 2000});
                        }else {
                            layer.msg(data.msg, {icon: 1, time: 2000});
                        }
                    },
                    error: function (data) {
                        layer.msg(data.msg, {icon: 1, time: 2000});
                        console.log(data.msg);
                    },
                });
            });
        }

        /*产品删除*/
        function product_del(obj, id) {
            layer.confirm('确认要删除该产品吗？', function (index) {
                $.ajax({
                    type: 'POST',
                    url: "{{url('admin/product')}}/"+id,
                    data:{'_method':'DELETE','_token':"{{csrf_token()}}"},
                    dataType: 'json',
                    success: function (data) {
                        if(data.status===200){
                            layer.msg('删除成功!', {icon: 1});
                            setTimeout(function () {
                                window.location.reload();
                            },1000)
                        }else {
                            layer.msg(data.msg, {icon: 2});
                        }
                    },
                    error: function (data) {
                        layer.msg(data.msg, {icon: 1});
                    },
                });
            });
        }
        
        /*上架*/
        function product_up(id,status) {
            layer.confirm('确定上架该产品吗？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                var _token="{{csrf_token()}}";
                $.post("{{url('admin/product_status')}}",{'_token':_token,'id':id,'status':status},function (res) {
                   if(res.status === 200){
                        layer.msg('上架成功', {icon: 1});
                        setTimeout(function () {
                            window.location.reload();
                        },1000)
                   } else {
                        layer.msg(res.msg, {icon: 2});
                   }
                });
            }, function(){
                layer.msg('取消上架');
            });
        }

        /*下架*/
        function product_down(id,status) {
            layer.confirm('确定下架该产品吗？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                var _token="{{csrf_token()}}";
                $.post("{{url('admin/product_status')}}",{'_token':_token,'id':id,'status':status},function (res) {
                    if(res.status === 200){
                        layer.msg('下架成功', {icon: 1});
                        setTimeout(function () {
                            window.location.reload();
                        },1000)
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                });
            }, function(){
                layer.msg('取消下架');
            });
        }

    </script>
@endsection